package com.shuoyue.ycgk.ui.mok;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Sourt;
import com.shuoyue.ycgk.entity.SourtInfo;
import com.shuoyue.ycgk.utils.GlideUtil;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MokSourtActivity extends BaseMvpActivity<MokSourtContract.Presenter>
        implements MokSourtContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.my_head)
    ImageView myHead;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.myScore)
    TextView myScore;
    @BindView(R.id.sort)
    TextView sort;
    @BindView(R.id.join_num)
    TextView joinNum;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.l_right)
    FrameLayout lRight;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    SortAdapter adapter;

    int id;
    ListWithPage listWithPage;
    @BindView(R.id.mine_card)
    CardView mineCard;


    public static void start(Context context, int mokId) {
        context.startActivity(new Intent(context, MokSourtActivity.class)
                .putExtra("id", mokId));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mok_sourt;
    }

    @Override
    protected void initView() {
        pageTitle.setText("排行榜");
        ivRight.setImageResource(R.mipmap.share_white);
        ivRight.setVisibility(View.VISIBLE);
        id = getIntent().getIntExtra("id", -1);
        recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4)));
        adapter = new SortAdapter(null);
        recycleView.setAdapter(adapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getSourt(id, 1);
            refreshLayout.finishRefresh();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getSourt(id, listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore();
        });
    }


    @Override
    public void initData() {
        super.initData();
        mPresenter = new MokSourtContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getSourt(id, 1);
    }


    @Override
    public void getSourtSuc(SourtInfo list) {
        Sourt myS = list.getMyScore();
        mineCard.setVisibility(myS == null ? View.GONE : View.VISIBLE);

        if (myS != null) {
            name.setText(myS.getUsername());
            myScore.setText(myS.getScore() + "");
            sort.setText(myS.getRank() + "");
            GlideUtil.loadImageCircular(mContext, myS.getAvatar(), R.mipmap.index_head, myHead);
        }

        listWithPage = list.getPage();
        if (list.getPage().getCurrent() == 1) {
            adapter.resetData(list.getPage().getRecords());
        } else {
            adapter.addData(list.getPage().getRecords());
        }
        joinNum.setText("共" + listWithPage.getTotal() + "参加");
        refreshLayout.setEnableLoadMore(list.getPage().getCurrent() != list.getPage().getPages()&& list.getPage().getPages() > 0);
    }


    @OnClick({R.id.l_right, R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.l_right:
                ShareUtil.shareUrl(this, ShareUtil.MOK_SORT_URL + id,
                        "模考排行",
                        "",
                        null);
//                ShareUtil.shareUrl(this, Constant.shareGift, null);
                break;
            case R.id.back: {
                finish();
                break;
            }
        }
    }

}